package com.chazz.servlet;/**
 *
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.chazz.dto.Result;
import com.chazz.entity.Category;
import com.chazz.service.CategoryidService;
import com.chazz.service.impl.CategoryidServiceImpl;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title: CategoryidServlet
 * @Author ZhaoHongRui
 * @Date: 2020/9/21 16:02
 * @Version 1.0
 */


public class CategoryidServlet extends BaseServlet {
    CategoryidService categoryidService = new CategoryidServiceImpl();

    /**
     * 商品的父分类
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void BigCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        System.out.println("进入BigCategory方法");

        String parentid = req.getParameter("id");
        System.out.println("parentid=" + parentid);
        List<List<Category>> listSmall = categoryidService.showSmallCategories();
        System.out.println("listSmall=" + listSmall);

        List<Category> listBig = categoryidService.showAllCategories();
        System.out.println("listBig=" + listBig);

        if (listSmall.size() > 0 && listBig.size() > 0) {
            System.out.println("商品查询成功");
        } else {
            System.out.println("商品查询失败");
        }
        Map<String, List> map = new HashMap<String, List>();
        map.put("listBig", listBig);
        map.put("listSmall", listSmall);
        resp.getWriter().write(JSON.toJSONString(map));


    }

    protected void getProductDataList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        String method = req.getParameter("method");
        PrintWriter out = resp.getWriter();
        if (method.equals("search")) {
            List<Category> categoryList = categoryidService.getCategoriesList(req.getParameter("searchValue"));
            String categoryListToJson = JSONArray.toJSONString(categoryList);
            out.write(categoryListToJson);
        }
    }


}
